package com.it.admin.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.it.admin.AdminProductService;
import com.it.dao.ProductGuestMapper;
import com.it.dao.ProductMapper;
import com.it.dao.ProductSkuMapper;
import com.it.pojo.Product;
//import com.it.pojo.ProductSku;
import com.it.pojo.ProductGuest;
import com.it.pojo.ProductSku;
import com.it.pojo.params.guest.AdminGuestQuery;
import com.it.pojo.params.product.AdminProductEditQuery;
import com.it.pojo.params.product.AdminProductQuery;
import com.it.pojo.params.product.AdminProductSaveQuery;
import com.it.pojo.vo.product.AdminProductEditVO;
import com.it.pojo.vo.product.AdminProductVO;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminProductServiceImpl implements AdminProductService {
    @Resource
    private ProductMapper productMapper;

    @Resource
    private ProductSkuMapper productSkuMapper;

    @Resource
    private ProductGuestMapper productGuestMapper;

    @Override
    public Map<String, Object> listAdminProductVO(AdminProductQuery adminProductQuery) {
        Integer limit = adminProductQuery.getLimit();
        Integer start = (adminProductQuery.getPageNum() - 1) * limit;
        Integer cityId = adminProductQuery.getCityId();
        Integer provinceId = adminProductQuery.getProvinceId();
        String startTime = null;

        String endTime = null;
        Map<String, Object> map = new HashMap<>();
        if (adminProductQuery.getDate().length != 0) {
            startTime = adminProductQuery.getDate()[0].substring(0, 10);
            endTime = adminProductQuery.getDate()[1].substring(0, 10);
        }
        List<AdminProductVO> list = productMapper.listAdminProductVO(provinceId, cityId, startTime, endTime, start, limit);

        Integer total = productMapper.listAdminProductVOTotal(provinceId, cityId, startTime, endTime, start, limit);
        map.put("ProductList", list);
        map.put("total", total);
        return map;
    }

    @Override
    @Transactional
    public int insertProduct(AdminProductSaveQuery adminProductQuery) {
        Product product = new Product();
        ProductSku productSku = new ProductSku();
//         当前时间
        Date date = new Date();

        String startTime = adminProductQuery.getDate()[0].substring(0, 10);
        String endTime = adminProductQuery.getDate()[1].substring(0, 10);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = null;
        Date endDate = null;
        try {
            startDate = simpleDateFormat.parse(startTime);
            endDate = simpleDateFormat.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        product.setTypeId(adminProductQuery.getTypeId());
        product.setGrade(adminProductQuery.getGrade());
        product.setAddress(adminProductQuery.getAddress());
        product.setProvinceId(adminProductQuery.getProvinceId());
        product.setProductName(adminProductQuery.getProductName());
        product.setCityId(adminProductQuery.getCityId());
        product.setCategoryId(adminProductQuery.getCategoryId());
        product.setGmtModified(date);
        product.setProductStatus(0);
        product.setGmtCreate(date);
        product.setDelayId(adminProductQuery.getDelayId());
        product.setPlatformId(adminProductQuery.getPlatformId());
        product.setVenueId(adminProductQuery.getVenueId());
        product.setTrafficId(adminProductQuery.getTrafficId());

        int i = productMapper.insert(product);


        List<AdminGuestQuery> guestList = adminProductQuery.getGuestList();
        guestList.forEach(e -> {
            ProductGuest productGuest = new ProductGuest();
            productGuest.setProductId(product.getProductId());
            productGuest.setGmtCreate(date);
            productGuest.setGmtModified(date);
            BeanUtils.copyProperties(e, productGuest);
            productGuest.setGuestImg(e.getImg());
            int insert = productGuestMapper.insert(productGuest);
        });
        productSku.setPrice(adminProductQuery.getPrice());
        productSku.setSellPrice(adminProductQuery.getSellPrice());
        productSku.setProductId(product.getProductId());
        productSku.setSkuImg(adminProductQuery.getImg());
        productSku.setStock(adminProductQuery.getStock());
        productSku.setSitePrice(adminProductQuery.getSitePrice());
        productSku.setStartTime(startDate);
        productSku.setEndTime(endDate);
        productSku.setGmtModified(date);

        productSku.setGmtCreate(date);
        int i1 = productSkuMapper.insert(productSku);
        return i + i1;
    }

    @Override
    @Transactional
    public int deleteProductById(Integer productId) {
        LambdaQueryWrapper<ProductSku> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ProductSku::getProductId, productId);
        int i1 = productSkuMapper.delete(wrapper);
        int i = productMapper.deleteById(productId);
        return i1 + i;
    }

    @Override
    public AdminProductEditVO getProduct(Integer productId) {
        return productMapper.getProductById(productId);

    }

    @Override
    @Transactional
    public int updateProductById(AdminProductEditQuery adminProductQuery) {
        ProductSku productSku = new ProductSku();
        Product product = new Product();
        Date date = new Date();
        List<AdminGuestQuery> guestList = adminProductQuery.getGuestList();
        LambdaQueryWrapper<ProductGuest> wrapper1 = Wrappers.lambdaQuery();
        wrapper1.eq(ProductGuest::getProductId, adminProductQuery.getProductId());
        productGuestMapper.delete(wrapper1);  // 先删除 后增加
        guestList.forEach(e -> {
            ProductGuest productGuest = new ProductGuest();
            BeanUtils.copyProperties(e, productGuest);
            productGuest.setProductId(adminProductQuery.getProductId());
            productGuest.setGmtModified(date);
            productGuest.setGuestImg(e.getImg());
            productGuestMapper.insert(productGuest);
        });
        if (adminProductQuery.getDate().length != 0) {
            String startTime = adminProductQuery.getDate()[0].substring(0, 10);
            String endTime = adminProductQuery.getDate()[1].substring(0, 10);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date startDate = simpleDateFormat.parse(startTime);
                Date endDate = simpleDateFormat.parse(endTime);
                productSku.setStartTime(startDate);
                productSku.setEndTime(endDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        BeanUtils.copyProperties(adminProductQuery, product);
        BeanUtils.copyProperties(adminProductQuery, productSku);
        LambdaQueryWrapper<ProductSku> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ProductSku::getProductId, product.getProductId());
        int i1 = productSkuMapper.update(productSku, wrapper);
        int i = productMapper.updateById(product);
        return i1 + i;
    }
}
